package com.controller;

import com.constant.Constant;
import com.entity.Course;
import com.github.pagehelper.PageInfo;
import com.service.CourseService;
import com.util.AjaxResult;
import com.vo.CourseVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * date:2021/4/30
 * description:
 * Author:Stargazer
 */
@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ResponseBody
    @RequestMapping("/loadRecCourse.do")
    public AjaxResult findThreeHotCourseByCourseId(HttpServletRequest request) {

        Integer course_id = Integer.parseInt(request.getParameter("course_id"));
        List<Course> threeCourseByCourseId = courseService.findThreeCourseByCourseId(course_id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setObj(threeCourseByCourseId);
        return ajaxResult;
    }

    /**
     * 根据条件分页查询课程
     */
    @RequestMapping("/findCourse.do")
    @ResponseBody
    public AjaxResult findCourseByCondition(@RequestBody CourseVo courseVo) {
        PageInfo<CourseVo> courseVos = courseService.findCourseByCourseVo(courseVo);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setObj(ajaxResult);

        return ajaxResult;
    }

    /**
     * 启用禁用课程
     */
    @RequestMapping("/toggle.do")
    @ResponseBody
    public void enableDisableCourse(Integer id, Integer status) {
        int i = courseService.modifyCourseById(id, status);
    }

    /**
     * 添加课程addCourse.do
     */
    @RequestMapping("addCourse.do")
    @ResponseBody
    public AjaxResult addCourse(@RequestParam("multiFiles") MultipartFile file, @RequestParam("course_name") String courseName,
                                String author, @RequestParam("recommendation_grade") Integer recommendationGrade,
                                @RequestParam("course_type_id") Integer courseTypeId, HttpSession session) throws IOException {
        Course course = this.getCourse(file, null, courseName, author, recommendationGrade, courseTypeId, session);

        int i = courseService.addCourseByCondition(course);
        System.out.println("i=" + i);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("添加课程成功");
        return ajaxResult;
    }

    /**
     * 修改课程前的查询方法
     * @param id
     * @return
     */
    @RequestMapping("/show.do")
    @ResponseBody
    public AjaxResult beforeCourse(Integer id){
        Course course = courseService.findCourseById(id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setObj(course);
        return ajaxResult;
    }

    /**
     * 修改课程的具体方法/modifyCourse.do
     */
    @RequestMapping("/modifyCourse.do")
    @ResponseBody
    public AjaxResult modifyCourse(@RequestParam("multiFiles") MultipartFile file, @RequestParam("id") Integer id, @RequestParam("course_name")String courseName,
                                   @RequestParam("author") String author, @RequestParam("recommendation_grade")Integer recommendationGrade,
                                   @RequestParam("course_type_id") Integer courseTypeId, HttpSession session)throws IOException{
        Course course = this.getCourse(file, id,courseName, author, recommendationGrade, courseTypeId, session);
        int i = courseService.modifyCourseByCondition(course);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("课程修改成功");
        return ajaxResult;
    }


    //跳转到章节管理页面
    @RequestMapping("/idHolder.do")
    public String jumpToChapter(Integer id, HttpSession session){
        session.setAttribute("id",id);
        return "background/back_courseReourceSet";
    }




    //封装Course对象,并将文件上传
    public Course getCourse(MultipartFile file,Integer id,String courseName,
                            String author,Integer recommendationGrade,
                            Integer courseTypeId,HttpSession session) throws IOException {
        //上传图片文件
        String path = session.getServletContext().getRealPath("/upload/course_cover/");
        System.out.println("path="+path);
        File file1 = new File(path);
        file1.mkdirs();
        String fileName = file.getOriginalFilename();
        file.transferTo(new File(file1,fileName));

        System.out.println("recommendationGrade="+recommendationGrade);
        Course course = new Course();
        course.setId(id);
        course.setCourse_name(courseName);
        course.setAuthor(author);
        course.setCover_image_url(fileName);
        course.setCreate_date(new Date());
        course.setClick_number(0);
        course.setStatus(Constant.STATUS_ENABLE);   //默认状态为0
        course.setRecommendation_grade(recommendationGrade);
        course.setCourse_type_id(courseTypeId);
        return course;
    }
}
